package com.iot.inclinometer.pojo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

public class QueryDataDTO {
    private String projectName = "";
    private String workpointName = "";
    private String drillName = "";
    private String startTime = "";
    private String endTime = "";
    private String measureDir = "";


    private int uploaded = -1;

    /**
     * 待导出的数据列表,以工点名称作为键key
     */
    private Vector<SendDataDTO> resultData;

    public QueryDataDTO() {
        resultData = new Vector<>();
    }

    public QueryDataDTO(String prjname, String wpName, String drillName, String startTime, String endTime, String measureDir) {
        this.projectName = prjname;
        this.workpointName = wpName;
        this.drillName = drillName;
        this.startTime = startTime;
        this.endTime = endTime;
        this.measureDir = measureDir;

        resultData = new Vector<>();
    }


    public void buildSendDataDTO(List<UploadDataInfo> uploadDataInfoList) {
        Map<String, String> drillWPNameMap = new HashMap<>();
        //每个钻孔所观测的组数
        Map<Integer, String> drillObserveGroupMap = new HashMap<>();
        //取出钻孔列表
        for (UploadDataInfo item : uploadDataInfoList) {
            String drillNo = item.getDrillNo();
            if (!drillWPNameMap.containsKey(drillNo)) {
                drillWPNameMap.put(drillNo, item.getWorkpointName());
            }

            int observeId = item.getObserverId();
            if (!drillObserveGroupMap.containsKey(observeId)) {
                // 一次观测对应一次时间
                drillObserveGroupMap.put(observeId, item.getTime());
            }
        }

        Set es = drillWPNameMap.entrySet();
        Iterator<Map.Entry<String, String>> iter = es.iterator();
        Map.Entry<String, String> entry = null;
        String tempWPName, tempDrillName;
        while (iter.hasNext()) {
            entry = iter.next();
            tempWPName = entry.getValue();
            tempDrillName = entry.getKey();

            SendDataDTO sendDataDTO = new SendDataDTO(this.projectName, tempWPName, tempDrillName);

            Set drillObsGroupSet = drillObserveGroupMap.entrySet();
            Iterator<Map.Entry<Integer, String>> obsGroupIter = drillObsGroupSet.iterator();
            Map.Entry<Integer, String> obsGroupEntry = null;
            String obsGroupTime;
            while(obsGroupIter.hasNext()){
                obsGroupEntry = obsGroupIter.next();
                obsGroupTime = null;
                SendDataDTO.DataBodyItemDTO dataBodyItemDTO = new SendDataDTO.DataBodyItemDTO();
                sendDataDTO.addDataItem(dataBodyItemDTO);

                //取出每一次观测的修正数据List
                for (UploadDataInfo item : uploadDataInfoList) {
                    // 取当前钻孔,当前观测组
                    if(tempWPName.equals(item.getWorkpointName()) && tempDrillName.equals(item.getDrillNo())
                    && obsGroupEntry.getKey().equals(item.getObserverId())){
                        if(null==obsGroupTime){
                            obsGroupTime = obsGroupEntry.getValue();
                            dataBodyItemDTO.setCollectTime(obsGroupTime);
                        }
                        dataBodyItemDTO.addDataItem(new SendDataDTO.DataBodyItemNodeDTO(
                                Double.parseDouble(item.getDepth()),
                                Double.parseDouble(item.getA0()),
                                Double.parseDouble(item.getB0()),
                                Double.parseDouble(item.getA180()),
                                Double.parseDouble(item.getB180())
                        ));
                    }// if
                }
            }// obsGroup
            resultData.add(sendDataDTO);//一个钻孔一条记录
        }
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getWorkpointName() {
        return workpointName;
    }

    public void setWorkpointName(String workpointName) {
        this.workpointName = workpointName;
    }

    public String getDrillName() {
        return drillName;
    }

    public void setDrillName(String drillName) {
        this.drillName = drillName;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getMeasureDir() {
        return measureDir;
    }

    public void setMeasureDir(String measureDir) {
        this.measureDir = measureDir;
    }

    public Vector<SendDataDTO> getResultData() {
        return resultData;
    }

    public void setResultData(Vector<SendDataDTO> resultData) {
        this.resultData = resultData;
    }


    public int getUploaded() {
        if (resultData == null || resultData.isEmpty()) {
            uploaded = -1;
        }

        boolean hasNotUploaded = false;
        for (SendDataDTO sendDataDTO : resultData) {
            if (sendDataDTO.getUploaded() < 0) {
                uploaded = -1;
                hasNotUploaded = true;
                break;
            }
        }
        uploaded = hasNotUploaded ? -1 : 1;

        return uploaded;
    }

    public void setUploaded(int uploaded) {
        this.uploaded = uploaded;
    }


    /**
     * 判断两个查询 条件对象是否 有重叠数据的可能（如：同一项目的工点钻孔的查询 时间区间重叠）
     *
     * @param target
     * @return
     */
    public boolean intersect(QueryDataDTO target) {


        return false;
    }

}
